﻿using System;
using System.Drawing;
using System.Collections.Generic;


namespace InsTron
{
    static class Settings
    {
        private static Dictionary<string, Size> resolutionSet;

        public static string CurrentResolutionKey { get; private set; }
        public static BikeParameters[] BikeParameters { get; private set; }
        public static bool Fullscreen { get; private set; }
        public static short SupersamplingFactor { get; private set; }
        public static bool PlatformReflection { get; private set; }


        public static void Initialize()
        {
            PrepareResoultionSet();
            CurrentResolutionKey = "1280x960";
            Fullscreen = false;
            SupersamplingFactor = 1;
            PlatformReflection = true;
            InitializeBikeParameters();
        }


        private static void InitializeBikeParameters()
        {
            BikeParameters = new BikeParameters[2];
            BikeParameters[0] = new BikeParameters
            {
                Color = Color.Red
            };
            BikeParameters[1] = new BikeParameters
            {
                Color = Color.Blue
            };
        }


        private static void PrepareResoultionSet()
        {
            resolutionSet = new Dictionary<string, Size>();
            resolutionSet.Add("1280x960", new Size(1280, 960)); // 4/3
            resolutionSet.Add("1280x800", new Size(1280, 800)); // 16/10
            resolutionSet.Add("1280x720", new Size(1280, 720)); // 16/9 HD
            resolutionSet.Add("1920x1080", new Size(1920, 1080)); // 16/9 fullHD
            resolutionSet.Add("1366x768", new Size(1366, 768)); // 16/9 laptop
        }


        public static Size CurrentResolution
        {
            get
            {
                try
                {
                    return resolutionSet[CurrentResolutionKey];
                }
                catch (KeyNotFoundException)
                {
                    return new Size(1280, 960);
                }
            }
        }


        public static Size WorkingResolution
        {
            get
            {
                return new Size(CurrentResolution.Width * SupersamplingFactor,
                    CurrentResolution.Height * SupersamplingFactor);
            }
        }


        public static void HdProfile()
        {
            CurrentResolutionKey = "1920x1080";
            Fullscreen = true;
            SupersamplingFactor = 2;
        }


        public static void LaptopProfile()
        {
            CurrentResolutionKey = "1366x768";
            Fullscreen = true;
            SupersamplingFactor = 2;
        }


        public static void DebugProfile()
        {
            CurrentResolutionKey = "1280x720";
            Fullscreen = false;
            SupersamplingFactor = 2;
        }


        public static List<string> GetResolutionKeysList()
        {
            return new List<string>(resolutionSet.Keys);
        }


        public static void CommitSettingsChange(bool fullscreen, string resolutionKey, bool supersampling, Color player1Color, Color player2Color, bool musicEnabled)
        {
            CommitGraphicsSettingsChange(fullscreen, resolutionKey, supersampling);
            CommitBikesParametersChange(player1Color, player2Color);
            CommitAudioSettingsChange(musicEnabled);
        }


        private static void CommitGraphicsSettingsChange(bool fullscreen, string resolutionKey, bool supersampling)
        {
            bool updateNeeded = !resolutionKey.Equals(CurrentResolutionKey) || fullscreen != Fullscreen;

            Fullscreen = fullscreen;
            CurrentResolutionKey = resolutionKey;
            SupersamplingFactor = (short)(supersampling ? 2 : 1);

            if (updateNeeded)
            {
                Program.UpdateRenderSettings();
            }
        }


        private static void CommitBikesParametersChange(Color player1Color, Color player2Color)
        {
            BikeParameters[0].Color = player1Color;
            BikeParameters[1].Color = player2Color;
        }


        private static void CommitAudioSettingsChange(bool musicEnabled)
        {
            // NOT IMPLEMENTED YET
        }
    }
}
